package com.ruoyi.labsafety.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.labsafety.domain.LabSafetyCheckIndexItem;
import com.ruoyi.labsafety.mapper.LabSafetyCheckIndexItemMapper;
import com.ruoyi.labsafety.service.ILabSafetyCheckIndexItemService;

/**
 * 安全检查指标项Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-06
 */
@Service
public class LabSafetyCheckIndexItemServiceImpl implements ILabSafetyCheckIndexItemService {
	@Autowired
	private LabSafetyCheckIndexItemMapper labSafetyCheckIndexItemMapper;

	/**
	 * 查询安全检查指标项
	 * 
	 * @param id
	 *            安全检查指标项ID
	 * @return 安全检查指标项
	 */
	@Override
	public LabSafetyCheckIndexItem selectLabSafetyCheckIndexItemById(Long id) {
		return labSafetyCheckIndexItemMapper.selectLabSafetyCheckIndexItemById(id);
	}

	/**
	 * 查询安全检查指标项列表
	 * 
	 * @param labSafetyCheckIndexItem
	 *            安全检查指标项
	 * @return 安全检查指标项
	 */
	@Override
	public List<LabSafetyCheckIndexItem> selectLabSafetyCheckIndexItemList(
			LabSafetyCheckIndexItem labSafetyCheckIndexItem) {
		return labSafetyCheckIndexItemMapper.selectLabSafetyCheckIndexItemList(labSafetyCheckIndexItem);
	}

	/**
	 * 新增安全检查指标项
	 * 
	 * @param labSafetyCheckIndexItem
	 *            安全检查指标项
	 * @return 结果
	 */
	@Override
	public int insertLabSafetyCheckIndexItem(LabSafetyCheckIndexItem labSafetyCheckIndexItem) {
		if (labSafetyCheckIndexItem.getParentId() != null) {
			LabSafetyCheckIndexItem info = labSafetyCheckIndexItemMapper
					.selectLabSafetyCheckIndexItemById(labSafetyCheckIndexItem.getParentId());
			if (info == null) {
				throw new CustomException("所选择的父节点不存在");
			}
			// 如果父节点不为正常状态,则不允许新增子节点
			
			 if (info.getStatusOpt()!=1) { 
				 throw new CustomException("父节点停用，不能增加子节点"); 
			 }
			labSafetyCheckIndexItem.setAncestors(info.getAncestors() + "," + labSafetyCheckIndexItem.getParentId());
		} else {
			labSafetyCheckIndexItem.setParentId(Long.valueOf(0));
			labSafetyCheckIndexItem.setAncestors("0");
		}
		return labSafetyCheckIndexItemMapper.insertLabSafetyCheckIndexItem(labSafetyCheckIndexItem);
	}

	/**
	 * 修改安全检查指标项
	 * 
	 * @param labSafetyCheckIndexItem
	 *            安全检查指标项
	 * @return 结果
	 */
	@Override
	public int updateLabSafetyCheckIndexItem(LabSafetyCheckIndexItem labSafetyCheckIndexItem) {
		LabSafetyCheckIndexItem newParentDept = labSafetyCheckIndexItemMapper
				.selectLabSafetyCheckIndexItemById(labSafetyCheckIndexItem.getParentId());
		LabSafetyCheckIndexItem oldDept = labSafetyCheckIndexItemMapper
				.selectLabSafetyCheckIndexItemById(labSafetyCheckIndexItem.getId());
		if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept)) {
			String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getId();
			String oldAncestors = oldDept.getAncestors();
			labSafetyCheckIndexItem.setAncestors(newAncestors);
			updateDeptChildren(labSafetyCheckIndexItem.getId(), newAncestors, oldAncestors);
		}
		return labSafetyCheckIndexItemMapper.updateLabSafetyCheckIndexItem(labSafetyCheckIndexItem);
	}

	/**
	 * 删除安全检查指标项信息
	 * 
	 * @param id
	 *            安全检查指标项ID
	 * @return 结果
	 */
	@Override
	public int deleteLabSafetyCheckIndexItemById(Long id) {
		return labSafetyCheckIndexItemMapper.deleteLabSafetyCheckIndexItemById(id);
	}

	/**
	 * 构建前端所需要树结构
	 * 
	 * @param depts
	 *            部门列表
	 * @return 树结构列表
	 */
	// @Override
	public List<LabSafetyCheckIndexItem> buildDeptTree(List<LabSafetyCheckIndexItem> depts) {
		List<LabSafetyCheckIndexItem> returnList = new ArrayList<LabSafetyCheckIndexItem>();
		List<Long> tempList = new ArrayList<Long>();
		for (LabSafetyCheckIndexItem dept : depts) {
			tempList.add(dept.getId());
		}
		for (Iterator<LabSafetyCheckIndexItem> iterator = depts.iterator(); iterator.hasNext();) {
			LabSafetyCheckIndexItem dept = (LabSafetyCheckIndexItem) iterator.next();
			// 如果是顶级节点, 遍历该父节点的所有子节点
			if (!tempList.contains(dept.getParentId())) {
				recursionFn(depts, dept);
				returnList.add(dept);
			}
		}
		if (returnList.isEmpty()) {
			returnList = depts;
		}
		return returnList;
	}

	/**
	 * 构建前端所需要下拉树结构
	 * 
	 * @param depts
	 *            部门列表
	 * @return 下拉树结构列表
	 */
	// @Override
	public List<TreeSelect> buildDeptTreeSelect(List<LabSafetyCheckIndexItem> depts) {
		List<LabSafetyCheckIndexItem> deptTrees = buildDeptTree(depts);
		return deptTrees.stream().map(TreeSelectCheckItem::new).collect(Collectors.toList());
	}

	/**
	 * 递归列表
	 */
	private void recursionFn(List<LabSafetyCheckIndexItem> list, LabSafetyCheckIndexItem t) {
		// 得到子节点列表
		List<LabSafetyCheckIndexItem> childList = getChildList(list, t);
		t.setChildren(childList);
		for (LabSafetyCheckIndexItem tChild : childList) {
			if (hasChild(list, tChild)) {
				recursionFn(list, tChild);
			}
		}
	}

	/**
	 * 得到子节点列表
	 */
	private List<LabSafetyCheckIndexItem> getChildList(List<LabSafetyCheckIndexItem> list, LabSafetyCheckIndexItem t) {
		List<LabSafetyCheckIndexItem> tlist = new ArrayList<LabSafetyCheckIndexItem>();
		Iterator<LabSafetyCheckIndexItem> it = list.iterator();
		while (it.hasNext()) {
			LabSafetyCheckIndexItem n = (LabSafetyCheckIndexItem) it.next();
			if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue()) {
				tlist.add(n);
			}
		}
		return tlist;
	}

	/**
	 * 判断是否有子节点
	 */
	private boolean hasChild(List<LabSafetyCheckIndexItem> list, LabSafetyCheckIndexItem t) {
		return getChildList(list, t).size() > 0 ? true : false;
	}

	/**
	 * 修改该部门的父级部门状态
	 * 
	 * @param dept
	 *            当前部门
	 */
	private void updateParentDeptStatus(LabSafetyCheckIndexItem entity) {
		String updateBy = entity.getUpdateBy();
		entity = labSafetyCheckIndexItemMapper.selectLabSafetyCheckIndexItemById(entity.getId());
		entity.setUpdateBy(updateBy);
		labSafetyCheckIndexItemMapper.updateStatus(entity);
	}

	/**
	 * 修改子元素关系
	 * 
	 * @param deptId
	 *            被修改的部门ID
	 * @param newAncestors
	 *            新的父ID集合
	 * @param oldAncestors
	 *            旧的父ID集合
	 */
	public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors) {
		List<LabSafetyCheckIndexItem> children = labSafetyCheckIndexItemMapper.selectChildrenById(deptId);
		for (LabSafetyCheckIndexItem child : children) {
			child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors));
		}
		if (children.size() > 0) {
			labSafetyCheckIndexItemMapper.updateChildren(children);
		}
	}
}
